package org.jeecg.modules.clue.controller;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.clue.entity.ClueRecord;
import org.jeecg.modules.clue.enums.RedisKey;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "公共数据")
@RestController
@RequestMapping("/clue/commonData")
@Slf4j
public class ClueCommonDataController {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    /**
     * 品牌车系
     * @return
     */
    @ApiOperation(value = "公共数据-品牌车系", notes = "公共数据-品牌车系")
    @GetMapping(value = "/brandSeries")
    public Result<JSONArray> brandSeries() {
        String data = stringRedisTemplate.opsForValue().get(RedisKey.CarBrandSeries.key);
        if (StringUtils.isNotBlank(data)) {
            return Result.ok(JSONArray.parseArray(data));
        }
        return Result.ok(new JSONArray());


    }
 /**
     * 根据车系获取车型
     * @return
     */
    @ApiOperation(value = "公共数据-根据车系获取车型", notes = "公共数据-根据车系获取车型")
    @GetMapping(value = "/getModelBySeriesId")
    public Result<JSONArray> getModelBySeriesId(@RequestParam String seriesid) {
        try {
            UrlBuilder urlBuilder = UrlBuilder.ofHttp("https://car-web-api.autohome.com.cn/car/series/getspeclistresponse", CharsetUtil.CHARSET_UTF_8)
                    .addQuery("seriesid", seriesid).addQuery("tagid", 1);
            HttpRequest request = HttpUtil.createGet(urlBuilder.toString());
            HttpResponse response = request.execute();
            String body = response.body();
            JSONObject bodyObject = JSONObject.parseObject(body);
            if (0 == bodyObject.getInteger("returncode")) {
                Object speclist0Obj = bodyObject.getJSONObject("result").getJSONObject("specinfo").getJSONArray("speclist")
                        .get(0);
                JSONObject speclist0Json = JSONObject.parseObject(JSONObject.toJSONString(speclist0Obj));
                return Result.ok(speclist0Json.getJSONArray("yearspeclist"));
            }
        } catch (HttpException e) {
            log.error("根据车系获取车型 失败 {}",e.getMessage());
        }


        return Result.ok(new JSONArray());


    }

    public static void main(String[] args) {
        ;

    }
}
